package com.fpt.edu.jornee;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.fpt.edu.jornee.account.RegisterFragment;
import com.fpt.edu.jornee.account.SettingFragment;
import com.fpt.edu.jornee.help.HelpFragment;
import com.fpt.edu.jornee.host.HostFragment;
import com.fpt.edu.jornee.location.LocationFragment;
import com.fpt.edu.jornee.messáge.MessageFragment;
import com.fpt.edu.jornee.outside.OutSideFragment;
import com.fpt.edu.jornee.sqllite.DatabaseHandler;
import com.fpt.edu.jornee.utils.MenuNavigationListAdapter;

public class MainActivity extends FragmentActivity {

	public static final String FUNCTION_NAME = "function name";

	private DrawerLayout mDrawerLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	DatabaseHandler databaseHandler = new DatabaseHandler(this);
	private CharSequence mDrawerTitle;
	private CharSequence mTitle;
	private String[] mMenuNavigation;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_layout);

		mTitle = mDrawerTitle = getTitle();
		mMenuNavigation = getResources().getStringArray(R.array.menuItems);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);

		// set a custom shadow that overlays the main content when the drawer
		// opens
		mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
				GravityCompat.START);
		// set up the drawer's list view with items and click listener
		ArrayList<HashMap<String, String>> functionNames = new ArrayList<HashMap<String, String>>();

		for (int i = 0; i < 9; i++) {

			HashMap<String, String> map = new HashMap<String, String>();

			switch (i) {
			case 0:
				break;
			case 1:
				map.put(FUNCTION_NAME, "Home");

				break;
			case 2:
				map.put(FUNCTION_NAME, "Outside");

				break;
			case 3:
				map.put(FUNCTION_NAME, "Message");

				break;
			case 4:
				map.put(FUNCTION_NAME, "Host");

				break;
			case 5:
				map.put(FUNCTION_NAME, "Location");

				break;
			case 6:
				map.put(FUNCTION_NAME, "Setting");

				break;
			case 7:
				map.put(FUNCTION_NAME, "Help");

				break;
			case 8:
				map.put(FUNCTION_NAME, "Logout");

				break;

			default:
				break;
			}

			functionNames.add(map);

			MenuNavigationListAdapter adapter = new MenuNavigationListAdapter(
					this, functionNames);

			mDrawerList.setAdapter(adapter);
		}
		mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
		// enable ActionBar app icon to behave as action to toggle nav drawer
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);

		// ActionBarDrawerToggle ties together the the proper interactions
		// between the sliding drawer and the action bar app icon
		mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
		mDrawerLayout, /* DrawerLayout object */
		R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
		R.string.drawer_open, /* "open drawer" description for accessibility */
		R.string.drawer_close /* "close drawer" description for accessibility */
		) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(mTitle);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(mDrawerTitle);
				invalidateOptionsMenu(); // creates call to
											// onPrepareOptionsMenu()
			}
		};
		mDrawerLayout.setDrawerListener(mDrawerToggle);

		if (savedInstanceState == null) {
			selectItem(0);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	/* Called whenever we call invalidateOptionsMenu() */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		// If the nav drawer is open, hide action items related to the content
		// view
		boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
		menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// The action bar home/up action should open or close the drawer.
		// ActionBarDrawerToggle will take care of this.
		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		// Handle action buttons
		switch (item.getItemId()) {
		case R.id.action_websearch:
			Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
			intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
			// catch event that there's no activity to handle intent
			if (intent.resolveActivity(getPackageManager()) != null) {
				startActivity(intent);
			} else {
				Toast.makeText(this, R.string.app_not_available,
						Toast.LENGTH_LONG).show();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/* The click listner for ListView in the navigation drawer */
	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			selectItem(position);
			Toast.makeText(MainActivity.this, "select " + position,
					Toast.LENGTH_LONG).show();
		}
	}

	private void selectItem(int position) {
		switch (position) {
		case 0:
			Fragment fragment0 = new RegisterFragment();
			FragmentManager fragmentManager0 = getSupportFragmentManager();
			fragmentManager0.beginTransaction()
					.replace(R.id.content_frame, fragment0).commit();

			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;

		case 1:
			finish();
			startActivity(getIntent());

			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 2:
			Fragment fragment = new OutSideFragment();

			FragmentManager fragmentManager = getSupportFragmentManager();
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 3:
			Fragment fragment1 = new MessageFragment();
			FragmentManager fragmentManager1 = getSupportFragmentManager();
			fragmentManager1.beginTransaction()
					.replace(R.id.content_frame, fragment1).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 4:
			Fragment fragment2 = new HostFragment();

			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content_frame, fragment2).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 5:
			Fragment fragment3 = new LocationFragment();
			FragmentManager fragmentManager3 = getSupportFragmentManager();
			fragmentManager3.beginTransaction()
					.replace(R.id.content_frame, fragment3).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 6:
			Fragment fragment4 = new SettingFragment();
			FragmentManager fragmentManager4 = getSupportFragmentManager();
			fragmentManager4.beginTransaction()
					.replace(R.id.content_frame, fragment4).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 7:
			Fragment fragment5 = new HelpFragment();
			FragmentManager fragmentManager5 = getSupportFragmentManager();
			fragmentManager5.beginTransaction()
					.replace(R.id.content_frame, fragment5).commit();

			// update selected item and title, then close the drawer
			mDrawerList.setItemChecked(position, true);
			setTitle(mMenuNavigation[position]);
			mDrawerLayout.closeDrawer(mDrawerList);
			break;
		case 8:

			break;

		default:
			break;
		}

	}

	@Override
	public void setTitle(CharSequence title) {
		mTitle = title;
		getActionBar().setTitle(mTitle);
	}

	/**
	 * When using the ActionBarDrawerToggle, you must call it during
	 * onPostCreate() and onConfigurationChanged()...
	 */

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		// Sync the toggle state after onRestoreInstanceState has occurred.
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		// Pass any configuration change to the drawer toggls
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

}
